#!/bin/bash

find_key_file="find_key.log-`basename $0`.$$"
rm -f $find_key_file &>/dev/null
# verbose=1
PATH=`pwd`:$PATH

PATCH_ELF_BFD=patch-elf-bfd

skip_file()
{
	if [ "${1##*.}" = "org" ]; then
		return 0
	fi
	return 1
}

function search_elf()
{
	soft_root=$1
	arch=$2
	exec_cmd=$3
	if [[ x$soft_root = x || ( x$arch != x64 && x$arch != x32 ) || x$exec_cmd = x ]]; then
		echo "search_elf paramter error: [$*]"
		exit 1
	fi
	shift;shift;shift
	exec_args=$*
	for fname in `find $soft_root -type f `
	do
		if(skip_file $fname); then
			continue;
		fi
		if (readelf -h $fname|& grep ELF$arch|grep "Class:" &>/dev/null); then
			$exec_cmd $fname $exec_args
		fi
	done
	return 0
}

_list_elf()
{
	fname=$1
	shift
	find_paramter=$*
	for key in `$PATCH_ELF_BFD $fname r $find_paramter |grep 'FKEY:'|awk '{print $3}'`
	do
		# echo $find_paramter
		echo "$key $fname $find_paramter"
		break
	done
}

list_elf()
{
	find_root_path=$1
	arch=$2
	shift;shift
	find_key_args=$*
	if [ x"$find_key_args" = x"" ]; then
		echo "paramter error"
		exit 1
	fi
	search_elf $find_root_path $arch _list_elf $find_key_args
}

_gen_key()
{
	fname=$1
	shift
	find_paramter=$*
	for key in `$PATCH_ELF_BFD $fname r $find_paramter |grep 'FKEY:'|awk '{print $3}'`
	do
		if [ x$verbose = x1 ]; then
			echo "$key $fname"
		fi
		echo $key >> $find_key_file.tmp
	done
}

gen_key()
{
	find_root_path=$1
	arch=$2
	shift;shift
	find_key_args=$*
	if [ x"$find_key_args" = x"" ]; then
		echo "paramter error"
		exit 1
	fi
	rm -f $find_key_file.tmp &>/dev/null

	search_elf $find_root_path $arch _gen_key $find_key_args

	cat $find_key_file.tmp|sort|uniq > $find_key_file
	rm $find_key_file.tmp &>/dev/null
	cat $find_key_file|head -n 3
	./gen_spec_key.py $find_key_file
	rm $find_key_file
}


_test_elf()
{
	fname=$1
	shift;
	find_paramter=$*
	$PATCH_ELF_BFD $fname rd $find_paramter
}

test_elf()
{
	find_root_path=$1
	arch=$2
	shift;shift
	key_args=$*
	search_elf $find_root_path $arch _test_elf  $key_args
}

_patch_elf()
{
	fname=$1
	shift;
	find_paramter=$*
	$PATCH_ELF_BFD $fname w $find_paramter
}

patch_elf()
{
	find_root_path=$1
	arch=$2
	shift;shift
	key_args=$*
	search_elf $find_root_path $arch _patch_elf  $key_args
}

soft_root=$1
arch=$2
cmd=$3
if [ x$cmd = x ]; then
	echo "usage: $0 soft_root arch command args"
	echo "command: [gen_key|test_elf|patch_elf|list_elf]"
	exit 1
fi
shift;shift;shift
$cmd $soft_root $arch $*

# # soft_root=/mnt/soft/eda/synopsys/verdi/Verdi_N-2017.12-SP2/platform/linux64
# soft_root=/home/slp/soft/synopsys/verdi/Verdi_N-2017.12-SP2
# arch=64

# echo "find key $arch scl_lc_ch_init"
# gen_key $soft_root $arch --func scl_lc_ch_init c3

# echo "find key $arch snsCheckOut"
# gen_key $soft_root $arch --func snsCheckOut 31c0c3


# echo "====patch x64===="
# # verdi 2017.12.sp2 x64
# patch_elf $soft_root 64 \
# --key 554889e54157415641554154534881ec482400004189d44c898598dbffff4c89 31c0c3 \
# --key 55488d15........4889e55de9......ff662e0f1f8400000000000f1f440000 c3

# echo "====patch x86===="
# # verdi 2017.12.sp2 x86
# patch_elf $soft_root 32 \
# --key 5589e557565381ec3c240000e88df9feff81c36d6098018b45088b7d108b4d20 31c0c3 \
# --key 5589e557565381ec3c2400008b45088b75108b55208995bcdbffff8b78148b08 31c0c3 \
# --key 5589e553e8........81c3........83ec148d83........894424088b450c89 c3 \
